/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <p>
 * Description: 缓存的是配置模板
 * ：不同的模板代表着不同的资源类别  但对模板的操作是相同的
 * ：此后如有新加的类别 只需添加模板文件就可以了
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-15 下午6:32:55          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ConfManager {

    private static final Logger logger = Logger.getLogger(ConfManager.class);

    private static ConfManager instance = null;

    //缓存(类别名称、类别),类别名称作为两个系统中资源类别的一个对应关系
    private Map<String, NetType> netNameMap = new HashMap<String, NetType>();

    //类别到网络设备的映射
    private Map<String, NetType> netTypeMap = new HashMap<String, NetType>();

    //缓存所有的的性能指标配置
    private List<HisPropConf> perfList = new ArrayList<HisPropConf>();

    //缓存所有的网络资源类别ID
    private Set<Long> typeSet = new HashSet<Long>();

    //缓存事件类别
    private Map<String, Long> eventTypeMap = new HashMap<String, Long>();

    //缓存资源类别到指标的映射， 实时性能处用
    private Map<Long, String> typePropsMap = new HashMap<Long, String>();

    private ConfManager() {
        initNetConf();
        initPerfConf();
    }

    /**
     * 因为要多次用到配置  用单例
     * @return
     */
    public static synchronized ConfManager getInstance() {
        if (instance == null) {
            instance = new ConfManager();
        }
        return instance;
    }

    /**
     * 初始类别缓存
     */
    @SuppressWarnings("unchecked")
    private void initNetConf() {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("inmp-conf/netmatch.xml");
            List<Element> elements = doc.selectNodes("/confList/typeList/type");
            for (Element e : elements) {
                NetType nt = new NetType();
                nt.setName(e.attributeValue("name").trim());
                nt.setType(e.attributeValue("typeId").trim());
                nt.setCpuType(e.elementText("cpu").trim());
                nt.setMemType(e.elementText("mem").trim());
                nt.setPortType(e.elementText("port").trim());

                typeSet.add(Long.parseLong(nt.getType()));
                netNameMap.put(nt.getName(), nt);
                netTypeMap.put(nt.getType(), nt);
            }

            List<Element> eventTypes = doc.selectNodes("/confList/eventList/type");
            for (Element e : eventTypes) {
                String name = e.attributeValue("name").trim();
                Long type = Long.parseLong(e.attributeValue("id").trim());
                eventTypeMap.put(name, type);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    /**
     * 缓存查询历史性能的sql
     */
    @SuppressWarnings("unchecked")
    private void initPerfConf() {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("inmp-conf/hisprop.xml");
            List<Element> elements = doc.selectNodes("/hisSql/conf");
            for (Element e : elements) {
                HisPropConf conf = new HisPropConf();
                conf.setSql(e.elementText("sql"));
                conf.setProps(e.elementText("props"));
                perfList.add(conf);
            }

            List<Element> types = doc.selectNodes("/hisSql/type-prop/conf");
            for (Element te : types) {
                Long type = Long.parseLong(te.attributeValue("type").trim());
                String ids = te.attributeValue("pids").trim();
                typePropsMap.put(type, ids);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public Map<String, NetType> getNetNameMap() {
        return netNameMap;
    }

    public Map<String, NetType> getNetTypeMap() {
        return netTypeMap;
    }

    public List<HisPropConf> getPerfList() {
        return perfList;
    }

    public Set<Long> getTypeSet() {
        return typeSet;
    }

    public Map<String, Long> getEventTypeMap() {
        return eventTypeMap;
    }

    public Map<Long, String> getTypePropsMap() {
        return typePropsMap;
    }

}
